home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / ansi / stdio / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-26  |  820 b   |  39 lines

  1. /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <libc/file.h>
  5.  
  6. size_t
  7. fread(void *vptr, size_t size, size_t count, FILE *iop)
  8. {
  9.   char *ptr = (char *)vptr;
  10.   int s;
  11.   int c;
  12.  
  13.   s = size * count;
  14.   while (s > 0) {
  15.     if (iop->_cnt < s) {
  16.       if (iop->_cnt > 0) {
  17.     memcpy(ptr, iop->_ptr, iop->_cnt);
  18.     ptr += iop->_cnt;
  19.     s -= iop->_cnt;
  20.       }
  21.       /*
  22.        * filbuf clobbers _cnt & _ptr,
  23.        * so don't waste time setting them.
  24.        */
  25.       if ((c = _filbuf(iop)) == EOF)
  26.     break;
  27.       *ptr++ = c;
  28.       s--;
  29.     }
  30.     if (iop->_cnt >= s) {
  31.       memcpy(ptr, iop->_ptr, s);
  32.       iop->_ptr += s;
  33.       iop->_cnt -= s;
  34.       return count;
  35.     }
  36.   }
  37.   return size != 0 ? count - ((s + size - 1) / size) : 0;
  38. }
  39.